﻿using System;
using System.Collections.Generic;

using Sunyi.Project.WebPoint.DataContract;

namespace Sunyi.Project.WebPoint.DataAccessController
{
    public partial class TokenDaoController
    {
        public Token SelectByUserName(string userName)
        {
            Token token = null;

            Dictionary<Guid, object> tokens = CacheProvider.Instance.Get<Token>();

            foreach (object obj in tokens.Values)
            {
                Token item = obj as Token;

                if (item != null && item.UserName == userName)
                {
                    token = item;
                    break;
                }
            }

            if (token == null)
            {
                token = dao.SelectByUserName(userName);

                if (token != null)
                {
                    CacheProvider.Instance.Add<Token>(token.Id, token);
                }
            }

            return token;
        }

        public void Erasure(Guid id)
        {
            dao.Erasure(id);
            CacheProvider.Instance.Remove<Token>(id);
        }
    }
}
